package com.xianbeiying.app.ckgl.photo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.photo.event.OnItemClickListener;
import com.xianbeiying.app.ckgl.photo.event.OnZoomListener;

import java.util.List;

/**
 * Created by wangeason on 15/9/22.
 */
public class HistoryPhotoGridAdapter extends RecyclerView.Adapter<HistoryPhotoGridAdapter.PhotoViewHolder> {
    private LayoutInflater inflater;
    private Context mContext;
    private OnItemClickListener onItemClickListener = null;
    private OnZoomListener onZoomListener = null;
    private List<String> photoDirectories;
    public HistoryPhotoGridAdapter(Context mContext, List<String> photoDirectories) {
        this.photoDirectories = photoDirectories;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }
    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_history_photo, parent, false);
        PhotoViewHolder holder = new PhotoViewHolder(itemView);
        return holder;
    }
    @Override
    public void onBindViewHolder(final PhotoViewHolder holder, final int position) {
//        Glide.with(mContext)
//                .load("http://"+ MyApplication.IP+ UrlBean.IMAGES+photoDirectories.get(position))
//                .centerCrop()
//                .thumbnail(0.1f)
//                .placeholder(R.drawable.ic_photo_black_48dp)
//                .error(R.drawable.ic_broken_image_black_48dp)
//                .into(holder.ivPhoto);
        ImageLoader.getInstance().displayImage("http://"+ MyApplication.IP+ UrlBean.IMAGES+photoDirectories.get(position),holder.ivPhoto);

        holder.ivPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onZoomListener != null) {
                    onZoomListener.onClick(view, position);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        int photosCount =
                photoDirectories.size();
        return photosCount;
    }
    public static class PhotoViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivPhoto;

        public PhotoViewHolder(View itemView) {
            super(itemView);
            ivPhoto = (ImageView) itemView.findViewById(R.id.iv_photo);
        }
    }
    public void setOnZoomListener(OnZoomListener onZoomListener) {
        this.onZoomListener = onZoomListener;
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
